<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<div id="container" style="width: 600px;height: 600px;"></div>
</body>
</html>
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.7&key=b019f71283af75d4ad241517faa931b4"></script>
<script>
    const radius = 10000
    var map = new AMap.Map('container', {
        resizeEnable: true
    });
    map.on('click', (e) => {
        map.clearMap();
        const lnglat = e.lnglat;
        var pos = [e.lnglat.lng, e.lnglat.lat];
        console.log(pos)
        addMarker(pos)
        circle(pos)
        const result = randomCircularPoint(lnglat.lat,lnglat.lng, radius);
        addMarker([result.lng, result.lat])
    })
    function onComplete(data) {
        // var position = data.position;
    }
    function onError(data) {
        console.log(data)
    }
    // 'AMap.ToolBar', 'AMap.CitySearch',
        map.plugin('AMap.Geolocation', function() {

        var geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,//是否使用高精度定位，默认:true
            buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
            zoomToAccuracy: true,      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
            buttonPosition:'RB',

        });
        map.addControl(geolocation)
        geolocation.getCurrentPosition();
        AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
        AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息


        // var citysearch = new AMap.CitySearch();
        //自动获取用户IP，返回当前城市
        // citysearch.getLocalCity(function (status, result) {
        //     // console.log(result)
        // })

    });
    // navigator.geolocation.getCurrentPosition((position) =>{
    //     console.log(position)
    // },(error) =>{
    //     console.log(error)
    // },{
    //     enableHighAccuracy:true, //boolean 是否要求高精度的地理信息，默认为false
    //     maximumAge:1000 //应用程序的缓存时间
    // })

    function addMarker(pos) {
       var marker = new AMap.Marker({
            icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
            position: pos
        });
        marker.setMap(map);
    }

    function circle(pos) {
        var circle = new AMap.Circle({
            center: pos,// 圆心位置
            radius: radius, //半径
            strokeColor: "#F33", //线颜色
            bubble: true,
            strokeOpacity: 0.6, //线透明度
            strokeWeight: 1, //线粗细度
            fillColor: "#ff9800", //填充颜色
            fillOpacity: 0.35//填充透明度
        });
        circle.setMap(map);
        // console.log(circle.contains(pos))
        // console.log(circle.getPath())
    }

    randomCircularPoint = (_lat, _lng, _radius) => {
        const { random, atan2, acos, asin, cos, sin, PI } = Math;
        const startLat = _lat * PI / 180; // 纬度 弧度
        const startLng = _lng * PI / 180; // 经度 弧度
        const radiusEarth = 6372.796924; // 地球半径km单位
        const maxDist = parseFloat(_radius) / 1000 / radiusEarth; // 弧度
        const rand1 = random();
        const rand2 = random();
        const dist = acos(rand1 * (cos(maxDist) - 1) + 1);
        const brg = 2 * PI * rand2;
        let lat = asin(sin(startLat) * cos(dist) + cos(startLat) * sin(dist) * cos(brg));
        let lng = startLng + atan2(sin(brg) * sin(dist) * cos(startLat), cos(dist) - sin(startLat) * sin(lat));
        if (lng < -PI) {
            lng = lng + 2 * PI;
        }
        if (lng > PI) {
            lng = lng - 2 * PI;
        }
        lat = lat * 180 / PI;
        lng = lng * 180 / PI;
        return {
            lat,
            lng
        }
    }
</script>